/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.share;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.framework.base.entity.SelectItem;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.share.CircleShare;
import com.boxin.ims.modules.wechat.service.share.CircleShareService;

/**
 * 分享转发Controller
 * @author Jakemanse
 * @version 2013-10-01
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/share/circleShare")
public class CircleShareController extends BaseController {

	@Autowired
	private CircleShareService circleShareService;
	
	@ModelAttribute
	public CircleShare get(@RequestParam(required=false) Long id) {
		if (id != null){
			return circleShareService.get(id);
		}else{
			return new CircleShare();
		}
	}
	
	@RequiresPermissions("wechat:share:circleShare:view")
	@RequestMapping(value = {"list", ""})
	public String list(CircleShare circleShare, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			circleShare.setUser(user);
		}
		
		if(circleShare.getStartTime() == null){
			Date now = DateUtils.getDayStartTime();
			circleShare.setStartTime(now);
			circleShare.setCreateTime(now);
		}else{
			circleShare.setCreateTime(circleShare.getStartTime() );
		}
		 
        Page<CircleShare> page = circleShareService.find(new Page<CircleShare>(request, response), circleShare); 
        model.addAttribute("page", page);
        model.addAttribute("circleShare", circleShare);
        
        
        List<SelectItem> selectItems =  SelectItem.getCancelstatuslist();
        model.addAttribute("selectItems", selectItems);
        
        
		return "modules/wechat/share/circleShareList";
	}

	@RequiresPermissions("wechat:share:circleShare:view")
	@RequestMapping(value = "form")
	public String form(CircleShare circleShare, Model model) {
		model.addAttribute("circleShare", circleShare);
		return "modules/wechat/share/circleShareForm";
	}

	@RequiresPermissions("wechat:share:circleShare:edit")
	@RequestMapping(value = "save")
	public String save(CircleShare circleShare, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, circleShare)){
			return form(circleShare, model);
		}
		circleShareService.save(circleShare);
		addMessage(redirectAttributes, "保存分享转发成功!");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/share/circleShare/?repage";
	}
	
	@RequiresPermissions("wechat:share:circleShare:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		circleShareService.delete(id);
		addMessage(redirectAttributes, "删除分享转发成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/share/circleShare/?repage";
	}
	
	
	@RequiresPermissions("wechat:share:circleShare:edit")
	@RequestMapping(value = "update")
	public String updateStatus(Long id,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String status = request.getParameter("status");
		circleShareService.updateStatusById(id, status) ;
		addMessage(redirectAttributes, ("1".equals(status)) ? "核销成功":"作废成功！");
		
		return "redirect:"+Global.ADMIN_PATH+"/wechat/share/circleShare/?repage";
	}


}
